package com.tbit.uqbike.object.value;

import com.alibaba.fastjson.annotation.JSONField;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Role;
import com.tbit.uqbike.webmanager.controller.interceptor.LanguageInterceptor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.web.servlet.support.RequestContext;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2019-12-05 17:35
 */
@Data
@NoArgsConstructor
@ToString
public class LoginInfo {
    @JSONField(serialize = false)
    public static final Integer DEF_ID = -1;
    @JSONField(serialize = false)
    public static final Integer DEF_TYPE = -1;
    // 用户id（这个是角色ID,即accountId）
    public Integer id = -1;
    // 用户类型（这个是角色类型,即accountType）
    public Integer type = -1;
    // 这个真实的账号ID（accountuser的主键id）
    public Integer accountUserId;
    // 品牌编号
    private Integer brandId;
    // 登录手机号
    public String phone;
    // 用户名
    public String name;
    // 角色
    public List<Role> roles;
    // 时区
    @JSONField(serialize = false)
    public Integer zone = LanguageInterceptor.DEF_ZONE;
    // 语言
    @JSONField(serialize = false)
    public Locale lang = LanguageInterceptor.EN_LOCAL;
    @JSONField(serialize = false)
    public RequestContext context;
    /**
     *  登录平台 1:小程序 2:管理端 3:运维端
     */
    private Integer  loginUserPlatform;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    public LoginInfo(Integer id, Integer type, Integer accountUserId) {
        this.id = id;
        this.type = type;
        this.accountUserId = accountUserId;
    }

    public LoginInfo(Integer id, Integer type, Integer accountUserId, String name, String phone) {
        this.id = id;
        this.type = type;
        this.accountUserId = accountUserId;
        this.phone = phone;
        this.name = name;
    }

    public LoginInfo(Integer id, Integer type, Integer accountUserId, String phone, String name, List<Role> roles, Integer brandId) {
        this.id = id;
        this.type = type;
        this.accountUserId = accountUserId;
        this.phone = phone;
        this.name = name;
        this.roles = roles;
        this.setLoginUserPlatform(OperationPlatform.MANAGER.code);
        this.brandId = brandId;
    }

    public LoginInfo(Integer accountUserId, String phone, String name) {
        this.accountUserId = accountUserId;
        this.phone = phone;
        this.name = name;
    }

    public LoginInfo(Integer accountUserId) {
        this.accountUserId = accountUserId;
    }

    /**
     * 当前角色是否是管理员
     *
     * @return true 是管理员 false 不是管理员
     */
    public boolean roleIsAdmin() {
        return type != null && 0 == type;
    }

    public boolean roleNotAdmin() {
        return type != null && 0 != type;
    }

    /**
     * 当前角色是否是品牌
     *
     * @return true 是品牌 false 不是品牌
     */
    public boolean roleIsBrand() {
        return UserConstant.USER_BRAND.equals(type);
    }

    /**
     * 当前角色是否是加盟商
     *
     * @return true 是加盟商 false 不是加盟商
     */
    public boolean roleIsFranchisee() {
        return UserConstant.USER_FRANCHISEE.equals(type);
    }

    /**
     * 当前角色是否是区域
     *
     * @return true 是区域 false 不是区域
     */
    public boolean roleIsAccount() {
        return UserConstant.USER_AGENT.equals(type);
    }

    public boolean roleLowerThanFranchisee() {
        return type != null && type >= UserConstant.USER_AGENT;
    }

    public boolean roleLowerThanBrand() {
        return type != null && type >= UserConstant.USER_FRANCHISEE;
    }

    // 刷新数据
    public void refreshZoneAndLang(Object lang, Object zone) {
        if (null != zone && zone instanceof Integer) {
            this.zone = (Integer) zone;
        }
        if (null != lang && lang instanceof Locale) {
            this.lang = (Locale) lang;
        }
    }

    // 检测是否登录了
    public boolean haveLogin() {
        return Objects.equals(DEF_ID, id) && Objects.equals(DEF_TYPE, type);
    }

    /**
     * 检查权限
     */
    public void requirePermission(String permission) {
        if (roleIsAdmin()) {
            return;
        }
        if (permissions == null || !permissions.contains(permission)) {
            throw new BaseException("权限不足");
        }
    }
}
